/*
 * GClubStats.java
 * Created on 21 sept. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2008, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.club;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.helper.GContestRoundType;
import com.hockeo.vhbase.data.helper.GStandingsRankType;

/**
 * Contains club related statistics. Its primary purpose is the counting of won titles,
 * promotions and relegations.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "h_club_stats" )
public class GClubStats implements IGEntity
{
  @Id
  @Column( name = "club_id" )
  private String d_strClubId;

  @Column( name = "num_league_titles" )
  private int    d_nNumLeagueTitles      = 0;

  @Column( name = "num_cup_titles" )
  private int    d_nNumCupTitles         = 0;

  @Column( name = "num_eu1_titles" )
  private int    d_nNumEU1Titles         = 0;

  @Column( name = "num_eu2_titles" )
  private int    d_nNumEU2Titles         = 0;

  @Column( name = "num_minor_league_titles" )
  private int    d_nNumMinorLeagueTitles = 0;

  @Column( name = "num_promotions" )
  private int    d_nNumPromotions        = 0;

  @Column( name = "num_relegations" )
  private int    d_nNumRelegations       = 0;

  /** Default parameterless constructor. */
  protected GClubStats()
  {}

  /**
   * Creates a new GClubStats object.
   *
   * @param club the club for which to create the object
   */
  public GClubStats( GClub club )
  {
    d_strClubId = club.getId();
    club.setStats( this );
  }

  /**
   * Adds new data based on the information obtained from the given club/season summary
   * object. Please make sure to not pass in the same summary object twice!
   *
   * @param summary the club/season summary object to be added
   */
  public void add( GClubSeasonSummary summary )
  {
    if ( summary.getLeagueRank() == 1 )
      if ( summary.getLeagueCategory() == 1 )
        d_nNumLeagueTitles++;
      else
        d_nNumMinorLeagueTitles++;

    if ( summary.getLeagueRankType() == GStandingsRankType.PROMOTED_1 ||
         summary.getLeagueRankType() == GStandingsRankType.PROMOTED_2 )
      d_nNumPromotions++;
    else if ( summary.getLeagueRankType() == GStandingsRankType.RELEGATED )
      d_nNumRelegations++;

    if ( summary.getCupReachedRound() == GContestRoundType.WINNER )
      d_nNumCupTitles++;

    if ( summary.getCLReachedRound() == GContestRoundType.WINNER )
      d_nNumEU1Titles++;

    if ( summary.getWLReachedRound() == GContestRoundType.WINNER )
      d_nNumEU2Titles++;
  }

  public int getNumLeagueTitles()
  {
    return d_nNumLeagueTitles;
  }

  public int getNumCupTitles()
  {
    return d_nNumCupTitles;
  }

  public int getNumEU1Titles()
  {
    return d_nNumEU1Titles;
  }

  public int getNumEU2Titles()
  {
    return d_nNumEU2Titles;
  }

  public int getNumMinorLeagueTitles()
  {
    return d_nNumMinorLeagueTitles;
  }

  public int getNumPromotions()
  {
    return d_nNumPromotions;
  }

  public int getNumRelegations()
  {
    return d_nNumRelegations;
  }

  public String getClubId()
  {
    return d_strClubId;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GClubStats.class;
  }

  public boolean instanceOf( Class<? extends IGEntity> classOther )
  {
    return classOther.isAssignableFrom( getGEntityClass() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_strClubId.equals( ( (GClubStats) obj ).getClubId() );
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_strClubId.hashCode();
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return "Statistics for club id " + d_strClubId;
  }
}
